<?php
namespace app\api\controller;
use think\Db;
use think\Image;
use think\Request;
use think\Session;
use think\Controller;
use app\api\controller\Base;

class Index extends Base
{
  	
  	//主页面显示(已测试)
    public function index()
    {
      //获取当前域名
        $request = Request::instance();
        $domain=$request->domain();
        $code=input('openid');
        $uid=Db::name('user')->where('code',$code)->field('uid')->find();

            $arr = Db::name('user')
                ->join('f_company','f_company.uid=f_user.uid')
                ->join('f_real','f_real.uid=f_user.uid')
                ->join('f_fans','f_fans.uid=f_user.uid')
                ->where('f_user.uid',$uid['uid'])
                ->field('f_user.nickname,f_user.avatars_img,f_user.uname,f_user.company,f_user.head_portrait,f_user.phone,f_user.gflg,f_company.*,f_real.rflg,f_fans.*,f_user.position')
                ->find();
            $rss = Db::name('company')->where('uid',$uid['uid'])->field('f_company.aid')->find();
          
            //如果该公司的企业能力为空的则为其赋值
            if($rss['aid']==''){
                $rss['aid']='暂无';
            }
            //有值的话则循环显示
            $rs = array_filter(explode(',',$rss['aid']));
            for($i=0;$i<count($rs);$i++)
            {
                $as[] = Db::name('ability')->where('aid',$rs[$i])->field('ability')->select();
            }
            $arr['aid'] = $as;
            $gua=array_filter(explode(',',$arr['fuid']));
          	//查询到粉丝总数
        	$fuid=Db::name('ribiao')->where('bid',$uid['uid'])->select();
        	//查询到关注总数
        	$arr['fuid'] = count(array_filter(explode(',',$arr['fuid'])));
        	$arr['guid'] = count(array_filter(explode(',',$arr['guid'])));
          //查询我的实名状态
          $q=Db::name('real')->where('uid',$uid['uid'])->field('rflg')->field('tid')->find();
          $arr['myrflg']=$q['rflg'];
          $arr['mytid']=$q['tid'];

      	if($arr['rflg'] == 1){
        	$arr['nickname']=$arr['uname'];
        }
        //判断用户头像是否为空为空则显示微信头像，如果两个头像都为空则为用户头像为空的添加一个临时头像（不存在数据库中的临时头像）
        if($uid['uid']!='')
        {
            if($arr['avatars_img']=='')
            {
                $rss=Db::name('user')->where('uid',$uid['uid'])->field('head_portrait')->find();
                $arr['avatars_img']=$rss['head_portrait'];
                if($arr['avatars_img']=='')
                {
                    $arr['avatars_img']=$domain.'/upload/linshiimg/20190411095739.jpg';
                }
            }else{
                $arr['avatars_img']=$domain.'/upload/portrait/'.$arr['avatars_img'];
            }
        }else{
            if($arr['avatars_img']=='')
            {
                $rss=Db::name('user')->where('uid',$uid['uid'])->field('head_portrait')->find();
                $arr['avatars_img']=$rss['head_portrait'];
                if($arr['avatars_img']=='')
                {
                    $arr['avatars_img']=$domain.'/upload/linshiimg/20190411095739.jpg';
                }
            }else{
                $arr['avatars_img']=$domain.'/upload/portrait/'.$arr['avatars_img'];
            }
        }
        //将四套图片分割成数组
        if($arr['location_img']!='')
        {
            $rc=array_filter(explode(',',$arr['location_img']));
            $rc1=count($rc);
            if($rc1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $e[]=$domain.'/upload/news/'.$rc[$i];
                    $arr['location_img']=$e;
                }
            }else{
                foreach($rc as $b)
                {
                    $box[]=$domain.'/upload/news/'.$b;
                    $arr['location_img']=$box;
                }
            }
        } else {
            $arr['location_img']=[];
        }
        if($arr['place_img']!='')
        {
            $ra=array_filter(explode(',',$arr['place_img']));
            $ra1=count($ra);
            if($ra1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $d[]=$domain.'/upload/news/'.$ra[$i];
                    $arr['place_img']=$d;
                }
            }else{
                foreach($ra as $b)
                {
                    $box1[]=$domain.'/upload/news/'.$b;
                    $arr['place_img']=$box1;
                }
            }
        }else{
            $arr['place_img']=[];
        }
        if($arr['equipment_img']!='')
        {
            $rb=array_filter(explode(',',$arr['equipment_img']));
            $rb1=count($rb);
            if($rb1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $c[]=$domain.'/upload/news/'.$rb[$i];
                    $arr['equipment_img']=$c;
                }
            }else{
                foreach($rb as $b)
                {
                    $box2[]=$domain.'/upload/news/'.$b;
                    $arr['equipment_img']=$box2;
                }
            }
        }else{
            $arr['equipment_img']=[];
        }
        if($arr['license_img']!='')
        {
            $rd=array_filter(explode(',',$arr['license_img']));
            $rd1=count($rd);
            if($rd1>6)
            {
                for($i=0;$i<6;$i++)
                {
                    $a[]=$domain.'/upload/news/'.$rd[$i];
                    $arr['license_img']=$a;
                }
            }else{
                foreach($rd as $b)
                {
                    $box3[]=$domain.'/upload/news/'.$b;
                    $arr['license_img']=$box3;
                }
            }
        }else{
            $arr['license_img']=[];
        }
        if($arr['location_img'] ==[] && $arr['place_img'] == [] && $arr['equipment_img'] == [] && $arr['license_img'] == []){
            $arr['tupian'] = 0;
        }else{
            $arr['tupian']=[$arr['location_img'],$arr['place_img'],$arr['equipment_img'],$arr['license_img']];
        }
        
		$arr[]=Db::name('user')->where('code',$code)->field('phone')->find();
      	
        return $this->AjaxReturn('1',$arr);
    }
   //自己产品信息的显示（已测试）
    public function message()
    {
      
       $request = Request::instance();
        $domain=$request->domain();
        $code=input('openid');
      	$page=input('page');
        $uid=Db::name('user')->where('code',$code)->field('uid')->find();
        $uid=$uid['uid'];
        $time=date('Y-m-d H:i:s',time());
          
      	$where['uid']=$uid;
        $where['mflg']=1;
        $xinxi=db('message')
                ->where($where)
                ->order('createtime desc')
          		->page($page,7)
                ->select();
        if($xinxi==[])
        {
            $xin='';
            return $xin;
        }else{
            for($i=0;$i<count($xinxi);$i++)
            {
                $xinxi[$i]['myuid'] = $uid;
                $rs[]=array_filter(explode(',',$xinxi[$i]['picture']));
              	$readNum=db('message')->where('mid',$xinxi[$i]['mid'])->value('num');
                $readNum = $readNum + 1;
                $readNum=db('message')->where('mid',$xinxi[$i]['mid'])->update(['num'=>$readNum]);
                if($xinxi[$i]['unit']==1)
                {
                    $xinxi[$i]['unit']='吨';
                }
                elseif($xinxi[$i]['unit']==2)
                {
                    $xinxi[$i]['unit']='公斤';
                }
                elseif($xinxi[$i]['unit']==3)
                {
                    $xinxi[$i]['unit']='斤';
                }elseif($xinxi[$i]['unit']==4)
                {
                    $xinxi[$i]['unit']='个';
                }
            }
            //只输出每组图片的第一个字符串并为其加上地址
            for($i=0;$i<count($rs);$i++)
            {
                if($rs[$i])
                {
                 	//$absolute= "/upload/suo/"; 
                 //	if (!is_dir(ROOT_PATH."/upload/suo/".$rs[$i][0]))
                  //	{
                  // 	$image = Image::open(ROOT_PATH."/upload/message/".$rs[$i][0]);
                   //	$pic = $image->thumb(500,500,Image:: THUMB_SCALING)->save(ROOT_PATH.$absolute.$rs[$i][0]);
                	// }
                   // $xinxi[$i]['picture']=$domain.$absolute.$rs[$i][0];
                    $xinxi[$i]['picture']='https://www.agrimanu.cn/upload/message/'.$rs[$i][0];
                }else{
                    $xinxi[$i]['picture']='https://www.agrimanu.cn/upload/message/no.jpg';
                }
            }
          
            return $this->AjaxReturn('1','操作成功',$xinxi);
        }
    }
    //产品信息的删除(测试)
    public function del()
    {
        $code=input('openid');
        $luid=Db::name('user')->where('code',$code)->field('uid')->find();
        $uid=$luid['uid'];
        $mid=input('mid');
        $del=Db::name('message')
            ->where('f_message.mid',$mid)
            ->update([
                'mflg'=>0
            ]);
        if($del){
            return $this->AjaxReturn('1','删除成功');
        }else{
            return $this->AjaxReturn('-1','信息错误');
        }
    }
    //粉丝(已测试)
    public function fen()
    {
        $uid=$this->uid;
        //得到前台传的值查询出这条数据
        $fens=Db::name('fans')
            ->where('uid',$uid)
            ->find();
        if($fens['fuid']!=='')
        {
            //去空并且分割guid,fuid
            $fs=array_filter(explode(',',$fens['fuid']));
            $gz=array_filter(explode(',',$fens['guid']));
            $fs=array_reverse($fs);
            //分割后循环出每个下标所对应的用户
            for($i=0;$i<count($fs);$i++)
            {
                $fen[]=Db::name('user')
                    ->join('f_real','f_real.uid=f_user.uid')
                    ->join('f_company','f_company.uid=f_user.uid')
                    ->where('f_user.uid',$fs[$i])
                    ->field('f_user.uid,f_user.nickname,f_user.uname,f_user.gflg,f_user.company,f_real.rflg,f_company.product')
                    ->select();
                if($fen[$i][0]['rflg']==1)
                {
                    $fen[$i][0]['nickname'] = $fen[$i][0]['uname'];
                }
                //in_array()函数：判断第一个参数是否存在于第二个数组中（第二参数需为数组）
                if(in_array($fen[$i][0]['uid'],$gz)){
                    $fen[$i][0]['fuid']='1';
                }else{
                    $fen[$i][0]['fuid']='0';
                }
            }
            return $this->AjaxReturn('1',$fen);
        }else{
         $m='';
         return $m;
        }
    }

    //关注(已测试)
    public function guan()
    {
        $uid=$this->uid;
        //得到前台传的值查询出这条数据
        $fens=Db::name('fans')
            ->where('f_fans.uid',$uid)
            ->find();
        if($fens['guid']!=='')
        {
            //去空并且分割guid,fuid
            $gz=array_filter(explode(',',$fens['guid']));
          	$gz=array_reverse($gz);
            //$fs=array_filter(explode(',',$fens['fuid']));
            //分割后循环出每个下标所对应的用户
            for($i=0;$i<count($gz);$i++)
            {
                $guan=Db::name('user')
                    ->join('f_real','f_real.uid=f_user.uid')
                    ->join('f_company','f_company.uid=f_user.uid')
                    ->where('f_user.uid',$gz[$i])
                    ->field('f_user.uid,f_user.gflg,f_user.nickname,f_user.uname,f_user.company,f_real.rflg,f_company.product')
                    ->select();
                $date[]=$guan;
                if($date[$i][0]['rflg']==1)
                {
                    $date[$i][0]['nickname'] = $date[$i][0]['uname'];
                }
 
                $date[$i][0]['fuid']='1';
                //dump($date[$i][0]['rflg']);die;
            }
            return $this->AjaxReturn('1',$date);
        }else{
            return null;
        }
    }

    //关注点击按钮(已测试)
    public function focus()
    {
        $time=date('Y-m-d H:i:s');
        //接收当前登陆着的openid
        $uid=$this->uid;
        $rss=Db::name('user')->where('uid',$uid)->field('uflg')->find();
        if($rss['uflg']==0)
        {
            return $this->AjaxReturn('-1','当前账户已被锁定');
        }
        //获取所要关注的uid
        $aid=input('aid');
        //先利用得到的两个id查询出每个id的关注和粉丝
        $arr=Db::name('fans')->where('uid',$uid)->find();
        $box=Db::name('fans')->where('uid',$aid)->find();
        $gz=array_filter(explode(',',$arr['guid']));
        if(in_array($aid,$gz))
        {
            return $this->AjaxReturn('-1','已关注，切勿重复关注');
        }
            //在字段原有的值后面追加得到的id并且利用逗号连接
        $rs=$arr['guid'].$aid.',';
        $re=$box['fuid'].$uid.',';
        //入库
        $guan=Db::name('fans')->where('uid',$uid)->update([
            'guid'=>$rs,
        ]);
        $fen=Db::name('fans')->where('uid',$aid)->update([
            'fuid'=>$re
        ]);
        //在日志表生成一条记录
        $rizhi=Db::name('ribiao')->insert([
            'uid'=>$uid,
            'bid'=>$aid,
            'createtime'=>$time,
            'zhaungtai'=>'0'
        ]);
        return $this->AjaxReturn('1','关注成功');
    }

    //取消关注点击按钮(已测试)
    public function nofocus()
    {
        //接收当前登陆着的openid
        $uid=$this->uid;
        //获取所要取消关注的uid
        $aid=input('aid');
        $rss=Db::name('user')->where('uid',$uid)->field('uflg')->find();
        //判断用户是否为黑名单
        if($rss['uflg']==0)
        {
            return $this->AjaxReturn('-1','当前账户已被锁定');
        }
        //if($uid || $aid)
        //{
        //    return $this->AjaxReturn('-1','关注失败');
        //}
        //先利用得到的两个id查询出每个id的关注和粉丝
        $arr=Db::name('fans')->where('uid',$uid)->find();
        $box=Db::name('fans')->where('uid',$aid)->find();
        //把当前登陆者关注字段利用逗号分割成数组
        $guid=array_filter(explode(',',$arr['guid']));
        if(!in_array($aid,$guid))
        {
            return $this->AjaxReturn('-1','未关注');
        }else{
            foreach($guid as $key=>$value)
            {
                if($aid==$value)
                {
                    unset($guid[$key]);
                }
            }
        }
        if(count($guid)=='')
        {
            $str='';
        }else{
            $str=implode(',',$guid).',';
        }
        //把所要取消关注者的粉丝字段利用逗号分割成数组
        $fuid=array_filter(explode(',',$box['fuid']));
        if(!in_array($uid,$fuid))
        {
            return $this->AjaxReturn('-1','未关注');
        }else{
            foreach ($fuid as $key=>$value)
            {
                if($uid==$value)
                {
                    unset($fuid[$key]);
                }
            }
        }
        //得到数组后转换为字符串
        if(count($fuid)==''){
            $string='';
        }else{
            $string=implode(',',$fuid).',';
        }
        //将修改后的关注字段替换到当前登陆者的guid下
        $guan=Db::name('fans')->where('uid',$uid)->update([
            'guid'=>$str,
        ]);
        //将修改后的粉丝字段替换到被取消关注下的fuid下
        $fen=Db::name('fans')->where('uid',$aid)->update([
            'fuid'=>$string,
        ]);
        //在日志表生成一条记录
        $where['uid']=$uid;
        $where['bid']=$aid;
        $del=Db::name('ribiao')->where($where)->delete();
        return $this->AjaxReturn('1','修改成功',$guan,$fen);
    }

    //查看改变粉丝状态
    public function zhuang()
    {
        $uid=$this->uid;
        $bid=input('bid');
        $fen=array_filter(explode(',',$bid));
        $where['bid']=$uid;
        foreach($fen as $v)
        {
            $where['uid']=$v;
            $arr=Db::name('ribiao')->where($where)->update(['zhaungtai'=>'1']);
        }
        return $this->AjaxReturn('1','操作成功',$arr);
    }

    //显示手机号(测试)
    public function phone()
    {
        $uid=$this->uid;
        $arr=Db::name('user')->where('uid',$uid)->field('f_user.phone')->find();
        return $this->AjaxReturn('1',$arr);
    }

    //套餐(测试)
    public function package()
    {
        $uid=$this->uid;
        $arr=Db::name('package')->where('flg',1)->order('tid asc')->select();
        $rs=Db::name('user')->where('uid',$uid)->field('avatars_img,head_portrait,nickname')->find();
        if($rs['avatars_img']=='')
        {
            $rs['avatars_img']=$rs['head_portrait'];
        }else{
            $rs['avatars_img']='https://www.agrimanu.cn/upload/portrait/'.$rs['avatars_img'];
        }
        return $this->AjaxReturn('1',$arr,$rs);
    }

    //特权显示
    public function privilege()
    {
        $arr=Db::name('privilege')->order('id asc')->select();
        return $this->AjaxReturn('1',$arr);
    }

    //设置页面(测试)
    public function shezhi()
    {
        $uid=$this->uid;
        $sub=input('sub');//判断是否点击保存按钮
        if(!null==$sub)
        {
            //接收用户头像的旧头像
            $image=input('image');
            //接收用户头像的新头像
            $img=input('img');
            //服务器中存放图片的文件夹名称
            $path='portrait';
            //生成一个随机数用来生成图片名称
            $rand=rand(1000,9999);
            //生成图片名称
            $pers=time().$rand.'.jpg';
            //文件夹目录
            $path ='upload'.DS.$path;
            //文件绝对路径
            $absolute=ROOT_PATH.$path.DS.$pers;
            //接收名称
            $name=input('uname');
          	//接收职务
            $position=input('position');
            //接收公司名称
            $company=input('company');
            //接收公司能力
            $aid=input('aid');
            //接收主营产品
            $product=input('product');
            //接收基本信息
            $information=input('information');
            //接收交付能力
            $ability=input('ability');
            //接收经营地址
            $address=input('address');
            //接收公司地址
            $introduce=input('introduce');
            //判断目录是否存在 不存在就创建
            if (!is_dir(ROOT_PATH.$path))
            {
                mkdir(ROOT_PATH.$path,0777,true);
            }
            //根据新头像的值是否为空判断是否修改头像
            if($img!='')
            {
                //根据旧头像是否为空判断是否第一次修改头像
                if($image!='')
                {
                    $fenge=explode('/',$image);
                    foreach ($fenge as $v){}
                    unlink(ROOT_PATH.'upload/portrait/'.$v);
                    $r = file_put_contents($absolute, base64_decode($img));
                    //修改用户表的信息
                    $ra=Db::name('user')->where('f_user.uid',$uid)->update([
                        'avatars_img'=>$pers,//头像
                        'nickname'=>$name,//姓名
                     	'position' => $position,//职务
                        'company' => $company,//公司名称
                    ]);
                    //修改公司表的信息
                    $rs=Db::name('company')->where('f_company.uid',$uid)->update([
                        'aid'=>$aid,//企业能力
                        'product'=>$product,//主营产品
                        'information'=>$information,//基本信息
                        'ability'=>$ability,//交付能力
                        'address'=>$address,//经营地址
                        'introduce'=>$introduce//公司介绍
                    ]);
                    if($rs){
                        return $this->AjaxReturn('1','修改成功');
                    }else{
                        return $this->AjaxReturn('-1','修改失败');
                    }
                }
                else {
                    //将图片解码并且上传到服务器中
                    $r = file_put_contents($absolute, base64_decode($img));
                    //修改用户表的信息
                    $ra = Db::name('user')->where('f_user.uid', $uid)->update([
                        'avatars_img' => $pers,//头像
                        'nickname' => $name,//姓名
                      	'position' => $position,//职务
                        'company' => $company,//公司名称
                    ]);
                    //修改公司表的信息
                    $rs = Db::name('company')->where('f_company.uid', $uid)->update([
                        'aid' => $aid,//企业能力
                        'product' => $product,//主营产品
                        'information' => $information,//基本信息
                        'ability' => $ability,//交付能力
                        'address' => $address,//经营地址
                        'introduce' => $introduce//公司介绍
                    ]);
                    if ($rs) {
                        return $this->AjaxReturn('1', '修改成功');
                    } else {
                        return $this->AjaxReturn('-1', '修改失败');
                    }
                }
            }
            else{
                $ra=Db::name('user')->where('f_user.uid',$uid)->update([
                    'nickname'=>$name,//姓名
                  	'position' => $position,//职务
                    'company' => $company,//公司名称
                ]);
                $rsc=Db::name('company')->where('f_company.uid',$uid)->update([
                    'aid'=>$aid,//企业能力
                    'product'=>$product,//主营产品
                    'information'=>$information,//基本信息
                    'ability'=>$ability,//交付能力
                    'address'=>$address,//经营地址
                    'introduce'=>$introduce//公司介绍
                ]);
                if($rsc ||  $ra){
                    return $this->AjaxReturn('1','修改成功');
                }else{
                    return $this->AjaxReturn('-1','修改失败');
                }
            }
        }
        else{
            //根据uid查询到基本要显示的信息
            $arr=Db::name('user')
                ->join('f_company','f_company.uid=f_user.uid')
                ->where('f_user.uid',$uid)
                ->field('f_user.avatars_img,f_user.nickname,f_user.uname,f_user.company,f_company.*,f_user.head_portrait,f_user.position')
                ->find();
            //判断用户名称是否为空为空则赋值一个空
            if($arr['nickname']=='')
            {
                $arr['nickname']= $arr['uname'];
            }
            //判断用户头像是否为空为空则显示微信头像，如果两个头像都为空则为用户头像为空的添加一个临时头像（不存在数据库中的临时头像）
            if($arr['avatars_img']!='')
            {
                $arr['avatars_img']='https://www.agrimanu.cn/upload/portrait/'.$arr['avatars_img'];
            }
            //将四套图片分割成数组
            if($arr['location_img']!='')
            {
                $rc=array_filter(explode(',',$arr['location_img']));
                $rc1=count($rc);
                if($rc1>6)
                {
                    for($i=0;$i<6;$i++)
                    {
                        $e[]='https://www.agrimanu.cn/upload/news/'.$rc[$i];
                        $arr['location_img']=$e;
                    }
                }else{
                    foreach($rc as $b)
                    {
                        $box[]='https://www.agrimanu.cn/upload/news/'.$b;
                        $arr['location_img']=$box;
                    }
                }
            } else {
                $arr['location_img']=[];
            }
            if($arr['place_img']!='')
            {
                $ra=array_filter(explode(',',$arr['place_img']));
                $ra1=count($ra);
                if($ra1>6)
                {
                    for($i=0;$i<6;$i++)
                    {
                        $d[]='https://www.agrimanu.cn/upload/news/'.$ra[$i];
                        $arr['place_img']=$d;
                    }
                }else{
                    foreach($ra as $b)
                    {
                        $box1[]='https://www.agrimanu.cn/upload/news/'.$b;
                        $arr['place_img']=$box1;
                    }
                }
            }else{
                $arr['place_img']=[];
            }
            if($arr['equipment_img']!='')
            {
                $rb=array_filter(explode(',',$arr['equipment_img']));
                $rb1=count($rb);
                if($rb1>6)
                {
                    for($i=0;$i<6;$i++)
                    {
                        $c[]='https://www.agrimanu.cn/upload/news/'.$rb[$i];
                        $arr['equipment_img']=$c;
                    }
                }else{
                    foreach($rb as $b)
                    {
                        $box2[]='https://www.agrimanu.cn/upload/news/'.$b;
                        $arr['equipment_img']=$box2;
                    }
                }
            }else{
                $arr['equipment_img']=[];
            }
            if($arr['license_img']!='')
            {
                $rd=array_filter(explode(',',$arr['license_img']));
                $rd1=count($rd);
                if($rd1>6)
                {
                    for($i=0;$i<6;$i++)
                    {
                        $a[]='https://www.agrimanu.cn/upload/news/'.$rd[$i];
                        $arr['license_img']=$a;
                    }
                }else{
                    foreach($rd as $b)
                    {
                        $box3[]='https://www.agrimanu.cn/upload/news/'.$b;
                        $arr['license_img']=$box3;
                    }
                }
            }else{
                $arr['license_img']=[];
            }
            //分割能力id
            $arr['aid']=array_filter(explode(',',$arr['aid']));
            //查询出能力表的所有能力
            $rs=Db::name('ability')->select();
            //追加到$arr中
            $arr['nengli']=$rs;
            return $this->AjaxReturn('1',$arr);
        }
    }

  
    //Base64图片上传
    public function Base()
    {
        //接收用户uid
        $uid=$this->uid;
        //接收最新的图片
        $image=input('image');
        $fen=array_filter(explode(',',$image));
        foreach ($fen as $v)
        {
            $name=input('name');
            //服务器中存放图片的文件夹名称
            $path='news';
            //生成一个随机数用来生成图片名称
            $rand=rand(1000,9999);
            //生成图片名称
            $pers=md5(time().$rand).'.jpg';
            //文件夹目录
            $path ='upload'.DS.$path;
            //文件绝对路径
            $absolute=ROOT_PATH.$path.DS.$pers;
            //判断目录是否存在 不存在就创建
            if (!is_dir(ROOT_PATH.$path))
            {
                mkdir(ROOT_PATH.$path,0777,true);
            }
            //根据字段名称查询出这个字段的值
            $arr=Db::name('company')->where('uid',$uid)->field($name)->find();
            //将图片名称存入数据库中去
            $rs=Db::name('company')->where('uid',$uid)->update([
                $name=>$arr[$name].$pers.','
            ]);
            //将图片上传到服务器上
            $r = file_put_contents($absolute, base64_decode($v));
        }
    }

    //删除图片
    public function file()
    {
        $uid=$this->uid;
        //获取要删除的图片名称
        $img=input('img');
        //获取要删除的图片所属字段
        $image1=input('name');
        //分割图片的绝地路径
        $image=explode('/',$img);
        //循环得到图片的名称
        foreach ($image as $v) {}
        //删除图片
        unlink(ROOT_PATH.'upload/news/'.$v);
        //查询出指定字段的值
        $arr=Db::name('company')->where('uid',$uid)->field($image1)->find();
        $location=array_filter(explode(',',$arr[$image1]));
        if(!in_array($v,$location))
        {
            return $this->AjaxReturn('-1','图片不存在');
        }else{
            foreach ($location as $key=>$value)
            {
                if($v==$value)
                {
                    unset($location[$key]);
                }
            }
        }
        if(count($location)=='')
       {
           $photo='';
       }else{
           $photo=implode(',',$location).',';
       }
        //修改数据库，将准备好的值从新赋值给指定字段
        $rs=Db::name('company')->where('uid',$uid)->update([
            $image1=>$photo
        ]);
        if($rs)
        {
            return $this->AjaxReturn('1','删除成功');
        }else{
            return $this->AjaxReturn('-1','删除失败');
        }
    }

    //判断是否是第二次实名认证
    public function reson()
    {
        $uid=$this->uid;
    	$rss=db('real')
          ->join('f_user','f_real.uid=f_user.uid','left')
          ->join('f_package','f_real.tid=f_package.tid','left')
          ->where('f_user.uid',$uid)
          ->select();
    	$url="https://www.agrimanu.cn/upload/real/";
    	$urls="https://www.agrimanu.cn/upload/portrait/";
       if($rss['0']['avatars_img']==""){
       	 $rss['0']['avatars_img']=$rss['0']['head_portrait'];
      }
      else{
      	$rss['0']['avatars_img']=$urls.$rss['0']['avatars_img'];
      }
    	$rss['0']['front_img']=$url.$rss['0']['front_img'];
    	$rss['0']['reverse_img']=$url.$rss['0']['reverse_img'];
    	if($rss)
    	{
    	    return $this->AjaxReturn("1","成功",$rss);
    	}else{
    	    return $this->AjaxReturn("-1","失败");
    	}
    }

    public function renzheng(){
      $uid=$this->uid;
      //查询用户是否购买套餐
      $ds=db('real')->where('uid',$uid)->value('tid');
      $uflg=db('user')->where('uid',$uid)->value('uflg');
      if($uflg==0){
      	 return $this->AjaxReturn("-2","该用户已被锁定");	
      }
      elseif($ds==0){
        return $this->AjaxReturn("-1","没有支付请支付");
      }
     else{
     	  $res=db('real')->where('uid',$uid)->value('rflg');
        if($res==0 ||$res==3){
             return $this->AjaxReturn("0","未认证");
        }
        elseif($res==4){
             return $this->AjaxReturn("4","认证中");
        }
        elseif($res==1){
             return $this->AjaxReturn("1","成功");
        }
         elseif($res==2){
             return $this->AjaxReturn("2","修改中");
        }	
     }
  }

    //实名认证
    public function shiming(){
        $uid=$this->uid;
          $rss=db('real')->where('uid',$uid)->field('front_img,reverse_img')->find();
          if($rss['front_img']==""&&$rss['front_img']=="")
          {
             $reson="";
             $times=date('Y-m-d H:i:s');
             $img_z=$this->add('jpg',input('image_z'),input('name_z'));
             $img_f=$this->add('jpg',input('image_f'),input('name_f'));
             $date=["card"=>input('card'),'Reason'=>$reson,'front_img'=>$img_z,'reverse_img'=>$img_f,'s_createtime'=>$times,'rflg'=>4];
             $res=db('real')->where('uid',$uid)->update($date);
             $rs=db('user')->where('uid',$uid)->update(['xname'=>input('uname')]);
              if($res||$rs){
                  return $this->AjaxReturn("1","操作成功");
              }else{
                  return $this->AjaxReturn("-1","操作失败");
              }
          }else{
          		 $front_img=input('front_img');
                 $front_img=basename($front_img);
                 $reverse_img=basename(input('reverse_img'));
            	 $reson="";
            	$img_z="";
                $img_f="";
           		 $times=date('Y-m-d H:i:s');
          		 $date=["card"=>input('card'),'Reason'=>$reson,'s_createtime'=>$times,'rflg'=>4];
            	 //判断数据库中身份证正面照是否存在存在则删除
                 if($rss['front_img']!=$front_img){
                      $path=ROOT_PATH.'upload/real/'.$rss['front_img'];
                      @unlink($path);   //删除图片
                      clearstatcache();//清缓存
                     $img_z=$this->add('jpg',input('image_z'),input('name_z'));
                  }
                 //判断数据库中身份证反面照是否存在存在则删除
                  if($rss['reverse_img']!=$reverse_img){
                      $path=ROOT_PATH.'upload/real/'.$rss['reverse_img'];
                      @unlink($path);   //删除图片
                      clearstatcache();//清缓存
                      $img_f=$this->add('jpg',input('image_f'),input('name_f'));
                  }
                 if($img_z!=""){
                      $date['front_img']=$img_z;
                 }
                if($img_f!=""){
                      $date['reverse_img']=$img_f;	
                }
            	 $res=db('real')->where('uid',$uid)->update($date);
                $rs=db('user')->where('uid',$uid)->update(['xname'=>input('uname')]);
                if($res||$rs){
                    return $this->AjaxReturn("1","操作成功");
                }else{
                    return $this->AjaxReturn("-1","操作失败");
                }
          }
    }
    public function shi(){
      $uid=$this->uid;
      $rss=db('user')
          ->join('f_real','f_user.uid=f_real.uid','left')
          ->join('f_package','f_package.tid=f_real.tid')
          ->where('f_user.uid',$uid)
           ->field('f_package.*,head_portrait,avatars_img,nickname')
          ->find();
       $urls="https://www.agrimanu.cn/upload/portrait/";
       if($rss['avatars_img']==""){
       	 $rss['avatars_img']=$rss['head_portrait'];
      }
      else{
      	$rss['avatars_img']=$urls.$rss['avatars_img'];
      }
      return $this->AjaxReturn("1","成功",$rss);
      
    }
    //实名认证的图片上传
    public function add($suffix,$image,$name)
    {
        //接收用户uid
        $uid=$this->uid;
        //服务器中存放图片的文件夹名称
        $path='real';
        //生成一个随机数用来生成图片名称
        $rand=rand(1000,9999);
        //生成图片名称
        $pers=time().$rand.'.'.$suffix;
        //文件夹目录
        $path ='upload'.DS.$path;
        //文件绝对路径
        $absolute=ROOT_PATH.$path.DS.$pers;
        //判断目录是否存在 不存在就创建
        if (!is_dir(ROOT_PATH.$path))
        {
            mkdir(ROOT_PATH.$path,0777,true);
        }
        $r = file_put_contents($absolute, base64_decode($image));
        return $pers;
    }

    //实名认证成功
    public function chenggong(){
     $uid=$this->uid;
    	$rss=db('real')
          ->join('f_user','f_real.uid=f_user.uid','left')
          ->where('f_user.uid',$uid)
          ->select();
           $url="https://www.agrimanu.cn/upload/real/";
           $urls="https://www.agrimanu.cn/upload/portrait/";
      if($rss['0']['avatars_img']==""){
       	 $rss['0']['avatars_img']=$rss['0']['head_portrait'];
      }
      else{
      	$rss['0']['avatars_img']=$urls.$rss['0']['avatars_img'];
      }
      $rss['0']['front_img']=$url.$rss['0']['front_img'];
      $rss['0']['reverse_img']=$url.$rss['0']['reverse_img'];
      if($rss){
      		return $this->AjaxReturn("1","成功",$rss);
      }else{
      		return $this->AjaxReturn("-1","失败",$rss);
      }
    }

    //实名认证失败
    public function shibai(){
     $uid=$this->uid;
      $rss=db('real')
        ->join('f_user','f_real.uid=f_user.uid','left')
        ->join('f_package','f_real.tid=f_package.tid','left')
        ->where('f_user.uid',$uid)
        ->field('f_package.phase_time,f_package.price,head_portrait,xname,front_img,reverse_img,card,uname,f_package.state,f_real.Reason,f_user.avatars_img,f_user.nickname')
        ->find();
        $urls="https://www.agrimanu.cn/upload/portrait/";
        $url="https://www.agrimanu.cn/upload/real/";
       if($rss['front_img']!=""){
       			$rss['front_img']=$url.$rss['front_img'];	
       }
      if($rss['reverse_img']){
      		$rss['reverse_img']=$url.$rss['reverse_img'];
      }
       if($rss['avatars_img']==""){
       	 $rss['avatars_img']=$rss['head_portrait'];
      }
      else{
      	$rss['avatars_img']=$urls.$rss['avatars_img'];
      }
      if($rss){
      		return $this->AjaxReturn("1","成功",$rss);
      }else{ 
      		return $this->AjaxReturn("-1","失败");
      }
    }
    public function formid(){
		$uid=$this->uid;
      	$formid=input('formId');
      	$time=time();
      	$endtime=time()+7*3600*24;
      	$date['uid']=$uid;
        $date['formid']=$formid;
        $date['endtime']=$endtime;
        $date['time']=$time;
      	$res=db('formid')
          ->insert($date);
    }
}
